package com.cxf.windex.rest;

import com.cxf.windex.base.rest.BaseApi;
import com.cxf.windex.biz.CxfPayInfoBiz;
import com.cxf.windex.biz.CxfPlatformBiz;
import com.cxf.windex.biz.PlatformUserBiz;
import com.cxf.windex.entity.CxfPayInfo;
import com.cxf.windex.entity.CxfPlatform;
import com.cxf.windex.entity.PlatformUser;
import com.cxf.windex.task.PayTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: drp
 * @description: 用户注册API
 * @author: chenxf
 * @create: 2018-05-23 13:46
 **/
@Api(tags = {"聚能小宝 - 平台API接口"})
@Controller
@RequestMapping("platform_api")
public class UserApi extends BaseApi {

  @Autowired
  private CxfPlatformBiz cxfPlatformBiz;
  @Autowired
  private PlatformUserBiz platformUserBiz;

  @Autowired
  private CxfPayInfoBiz cxfPayInfoBiz;

  @Autowired
  private PayTask payTask;

  /**
   * 根据appid 生成token
   */

  public Map getToken(String appid) {
    return new HashMap();
  }

  /**
   * 保存用户信息
   *
   * @param platformUser 平台用户信息
   */
  @ApiOperation(value = "保存推广用户信息", notes = "保存推广用户信息")
  @RequestMapping(value = "/saveUser", method = RequestMethod.POST)
  @ResponseBody
  public Map saveUser(@RequestBody PlatformUser platformUser) {
    if (StringUtils.isBlank(platformUser.getToken())) {
      logger.error("token={} 不存在", platformUser.getToken());
      return resultMap("-1", "请传入token");
    }
    CxfPlatform cxfPlatform = new CxfPlatform();
    cxfPlatform.setAppid(platformUser.getToken());
    cxfPlatform = cxfPlatformBiz.selectOne(cxfPlatform);
    if (cxfPlatform == null) {
      logger.error("平台={} 未接入", platformUser.getToken());
      return resultMap("-1", "平台未接入");
    }
    platformUser.setPlatform(cxfPlatform.getId());
    platformUser.setPlatformName(cxfPlatform.getName());
    logger.debug("crtTime={}",platformUser.getAddtime());
    platformUserBiz.insertSelective(platformUser);

    return resultMap("0", "保存成功");
  }

  /**
   * 保存支付信息
   *
   * @param cxfPayInfo 支付信息
   */
  @ApiOperation(value = "保存用户支付信息", notes = "保存用户支付信息")
  @RequestMapping(value = "/savePay", method = RequestMethod.POST)
  @ResponseBody
  public Map savePay(@RequestBody CxfPayInfo cxfPayInfo) {
    cxfPayInfoBiz.insertSelective(cxfPayInfo);

    //查询平台用户
    PlatformUser platformUser = new PlatformUser();
    platformUser.setUserid(cxfPayInfo.getUserid());
    platformUser = platformUserBiz.selectOne(platformUser);
    if (platformUser == null) {
      logger.error("找不到对应的平台用户");
    } else {
      platformUser.setAddress(cxfPayInfo.getAddress());
      platformUserBiz.updateSelectiveById(platformUser);
      if (StringUtils.isNotBlank(platformUser.getCrtUser())) {
        payTask.doPayTask(platformUser, cxfPayInfo);
      } else {
        logger.info("无销售人员");
      }
    }

    resultMap.put("code", "0");
    resultMap.put("message", "保存成功");
    return resultMap;
  }
}
